﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace IGolovkine.Core.Collections.Specialized
{
    public class ObservableList<T> : INotifyCollectionChanged, IList<T>, ICollection<T>
    {
        public event NotifyCollectionChangedEventHandler CollectionChanged;

        private readonly List<T> _list;
        public ObservableList()
        {
            _list = new List<T>();
        }

        public ObservableList(IEnumerable<T> collection)
        {
            _list = new List<T>(collection);
        }

        public ObservableList(int capacity)
        {
            _list = new List<T>(capacity);
        }

        public int IndexOf(T item)
        {
            return _list.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            _list.Insert(index, item);
            this.OnListChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void RemoveAt(int index)
        {
            _list.RemoveAt(index);
            this.OnListChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public T this[int index]
        {
            get { return _list[index]; }
            set
            {
                if (!object.Equals(_list[index], value))
                {
                    _list[index] = value;
                    this.OnListChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                }
            }
        }

        public void Add(T item)
        {
            _list.Add(item);
            this.OnListChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void Clear()
        {
            if (_list.Count != 0)
            {
                _list.Clear();
                this.OnListChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }

        public bool Contains(T item)
        {
            return _list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _list.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {

            bool removed = _list.Remove(item);
            if (removed)
            {
                this.OnListChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
            return removed;
        }

        public void RemoveAll(Predicate<T> match)
        {
            _list.RemoveAll(match);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        protected virtual void OnListChanged(NotifyCollectionChangedEventArgs args)
        {
            if (this.CollectionChanged != null)
            {
                this.CollectionChanged(this, args);
            }
        }
    }
}

